/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bhatni.taskmanager.task;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author virendra
 */
public class ExecTask extends TaskBase {

    private String[] commnadArray;
    private String[] envVariablesArray;

    private String commands;
    private String envVariables;
    private String workingDirectory;
    
    @Override
    public void run() {

        try {
            File workDir = null;
            if (workingDirectory != null)
                workDir = new File(workingDirectory);
            Process process = Runtime.getRuntime().exec(commnadArray, envVariablesArray, workDir);
            BufferedReader in = new BufferedReader(
                    new InputStreamReader(process.getInputStream()));
            String line = null;
            while ((line = in.readLine()) != null) {
                System.out.println(line);
            }

        } catch (IOException ex) {
            Logger.getLogger(ExecTask.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * @return the commands
     */
    public String getCommands() {
        return commands;
    }

    /**
     * @param commands the commands to set
     */
    public void setCommands(String commands) {
        this.commands = commands;
        commnadArray = this.commands.split(";");
    }

    /**
     * @return the envVariables
     */
    public String getEnvVariables() {
        return envVariables;
    }

    /**
     * @param envVariables the envVariables to set
     */
    public void setEnvVariables(String envVariables) {
        this.envVariables = envVariables;
        envVariablesArray = this.envVariables.split(";");
}

    /**
     * @return the workingDirectory
     */
    public String getWorkingDirectory() {
        return workingDirectory;
    }

    /**
     * @param workingDirectory the workingDirectory to set
     */
    public void setWorkingDirectory(String workingDirectory) {
        this.workingDirectory = workingDirectory;
    }
}
